home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / lightning-0.8-tb-win.xpi / js / calProviderBase.js < prev    next >
Text File  |  2008-03-13  |  9KB  |  243 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Sun Microsystems code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  *   Philipp Kewisch <mozilla@kewis.ch>
  18.  * Portions created by the Initial Developer are Copyright (C) 2007
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Daniel Boelzle <daniel.boelzle@sun.com>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. function calProviderBase() {
  39.     ASSERT("This prototype should only be inherited!");
  40. }
  41.  
  42. calProviderBase.prototype = {
  43.     QueryInterface: function cPB_QueryInterface(aIID) {
  44.         return doQueryInterface(this, calProviderBase.prototype, aIID,
  45.                                 [Components.interfaces.nsISupports,
  46.                                  Components.interfaces.calICalendar]);
  47.     },
  48.  
  49.     mID: null,
  50.     mUri: null,
  51.     mObservers: null,
  52.     mProperties: null,
  53.  
  54.     initProviderBase: function cPB_initProviderBase() {
  55.         this.wrappedJSObject = this;
  56.         this.mObservers = new calListenerBag(Components.interfaces.calIObserver);
  57.         this.mProperties = {};
  58.     },
  59.  
  60.     get observers() {
  61.         return this.mObservers;
  62.     },
  63.  
  64.     // attribute AUTF8String id;
  65.     get id() {
  66.         return this.mID;
  67.     },
  68.     set id(aValue) {
  69.         if (this.mID) {
  70.             throw Components.results.NS_ERROR_ALREADY_INITIALIZED;
  71.         }
  72.         this.mID = aValue;
  73.  
  74. //         ASSERT(this.mProperties.toSource() == "({})", "setProperty calls before id has been set!");
  75.  
  76.         // xxx todo: move this code hack when migrating storage prefs to moz prefs,
  77.         //           presumably with bug 378754
  78.         var calMgr = getCalendarManager();
  79.         var this_ = this;
  80.         function takeOverIfNotPresent(oldPref, newPref, dontDeleteOldPref) {
  81.             var val = calMgr.getCalendarPref_(this_, oldPref);
  82.             if (val !== null) {
  83.                 if (!dontDeleteOldPref) {
  84.                     calMgr.deleteCalendarPref_(this_, oldPref);
  85.                 }
  86.                 if (calMgr.getCalendarPref_(this_, newPref) === null) {
  87.                     calMgr.setCalendarPref_(this_, newPref, val);
  88.                 }
  89.             }
  90.         }
  91.         // takeover lightning calendar visibility from 0.5:
  92.         takeOverIfNotPresent("lightning-main-in-composite", "calendar-main-in-composite");
  93.         takeOverIfNotPresent("lightning-main-default", "calendar-main-default");
  94.  
  95.         return aValue;
  96.     },
  97.  
  98.     // attribute AUTF8String name;
  99.     get name() {
  100.         return this.getProperty("name");
  101.     },
  102.     set name(aValue) {
  103.         return this.setProperty("name", aValue);
  104.     },
  105.  
  106.     // attribute calICalendar superCalendar;
  107.     get superCalendar() {
  108.         // If we have a superCalendar, check this calendar for a superCalendar.
  109.         // This will make sure the topmost calendar is returned
  110.         return (this.mSuperCalendar ? this.mSuperCalendar.superCalendar : this);
  111.     },
  112.     set superCalendar(val) {
  113.         return (this.mSuperCalendar = val);
  114.     },
  115.  
  116.     // attribute nsIURI uri;
  117.     get uri() {
  118.         return this.mUri;
  119.     },
  120.     set uri(aValue) {
  121.         return (this.mUri = aValue);
  122.     },
  123.  
  124.     // attribute boolean readOnly;
  125.     get readOnly() {
  126.         return this.getProperty("readOnly");
  127.     },
  128.     set readOnly(aValue) {
  129.         return this.setProperty("readOnly", aValue);
  130.     },
  131.  
  132.     // readonly attribute boolean canRefresh;
  133.     get canRefresh() {
  134.         return false;
  135.     },
  136.  
  137.     // readonly attribute boolean sendItipInvitations;
  138.     get sendItipInvitations() {
  139.         return true;
  140.     },
  141.  
  142.     // void startBatch();
  143.     startBatch: function cPB_startBatch() {
  144.         this.mObservers.notify("onStartBatch");
  145.     },
  146.  
  147.     endBatch: function cPB_endBatch() {
  148.         this.mObservers.notify("onEndBatch");
  149.     },
  150.  
  151.     // nsIVariant getProperty(in AUTF8String aName);
  152.     getProperty: function cPB_getProperty(aName) {
  153.         var ret = this.mProperties[aName];
  154.         if (ret === undefined) {
  155.             ret = null;
  156.             if (this.id) {
  157.                 // xxx future: return getPrefSafe("calendars." + this.id + "." + aName, null);
  158.                 ret = getCalendarManager().getCalendarPref_(this, aName);
  159.                 if (ret !== null) {
  160.                     // xxx todo: work around value types here unless we save into the prefs...
  161.                     switch (aName) {
  162.                         case "suppressAlarms":
  163.                             if (this.getProperty("capabilities.alarms.popup.supported") === false) {
  164.                                 // If popup alarms are not supported,
  165.                                 // automatically suppress alarms
  166.                                 ret = true;
  167.                                 break;
  168.                             }
  169.                             // Otherwise fall through to fix the type
  170.                         case "readOnly":
  171.                         case "relaxedMode":
  172.                         case "cache.supported":
  173.                         case "cache.enabled":
  174.                         case "calendar-main-in-composite":
  175.                         case "calendar-main-default":
  176.                             ret = (ret == "true");
  177.                             break;
  178.                         case "backup-time":
  179.                         case "cache.updateTimer":
  180.                             ret = Number(ret);
  181.                             break;
  182.                     }
  183.                 }
  184.             }
  185.             this.mProperties[aName] = ret;
  186.         }
  187.         return ret;
  188.     },
  189.  
  190.     // void setProperty(in AUTF8String aName, in nsIVariant aValue);
  191.     setProperty: function cPB_setProperty(aName, aValue) {
  192.         var oldValue = this.getProperty(aName);
  193.         if (oldValue != aValue) {
  194.             this.mProperties[aName] = aValue;
  195.             if (this.id) {
  196.                 var v = aValue;
  197.                 // xxx todo: work around value types here unless we save into the prefs...
  198.                 switch (aName) {
  199.                 case "readOnly":
  200.                 case "relaxedMode":
  201.                 case "cache.supported":
  202.                 case "cache.enabled":
  203.                 case "suppressAlarms":
  204.                 case "calendar-main-in-composite":
  205.                 case "calendar-main-default":
  206.                     v = (v ? "true" : "false");
  207.                     break;
  208. //                 case "backup-time":
  209. //                 case "cache.updateTimer":
  210. //                     break;
  211.                 }
  212.                 // xxx future: setPrefSafe("calendars." + this.id + "." + aName, aValue);
  213.                 getCalendarManager().setCalendarPref_(this, aName, v);
  214.             }
  215.             this.mObservers.notify("onPropertyChanged",
  216.                                    [this.superCalendar, aName, aValue, oldValue]);
  217.         }
  218.         return aValue;
  219.     },
  220.  
  221.     // void deleteProperty(in AUTF8String aName);
  222.     deleteProperty: function cPB_deleteProperty(aName) {
  223.         this.mObservers.notify("onPropertyDeleting", [this.superCalendar, aName]);
  224.         delete this.mProperties[aName];
  225.         getCalendarManager().deleteCalendarPref_(this, aName);
  226.     },
  227.  
  228.     // calIOperation refresh
  229.     refresh: function cPB_refresh() {
  230.         return null;
  231.     },
  232.  
  233.     // void addObserver( in calIObserver observer );
  234.     addObserver: function cPB_addObserver(aObserver) {
  235.         this.mObservers.add(aObserver);
  236.     },
  237.  
  238.     // void removeObserver( in calIObserver observer );
  239.     removeObserver: function cPB_removeObserver(aObserver) {
  240.         this.mObservers.remove(aObserver);
  241.     }
  242. };
  243.